<?php

namespace App\Http\Controllers\Api\Room;

use App\Http\Controllers\Controller;
use App\Libs\Strategy\Context;
use App\Models\Rooms\Room;
use App\Models\Rooms\RoomGoods;
use App\Models\Rooms\RoomsLevel;
use Illuminate\Http\Request;

class RoomGoodsController extends Controller
{
    private $rgoods;

   public function __construct(){
        $this->rgoods = new Context(new RoomGoods());
    }

    public function index(Request $request)
    {

        $roomLevels = RoomsLevel::get();

        $roomGoods = $this->rgoods->display(request()->limit,function($query){
            $query->with('rooms','member','guess_goods');
            if(request()->keyword)
            {
                $ids = array_column(Room::where('room_name',request()->keyword)->get(['id'])->toArray(), 'id');
                if(!empty($ids))
                {
                    $query->whereIn('id',$ids);
                }else{
                    $query->where('room_num',request()->keyword);
                }
            }
            if(request()->level)
            {
                 $query->where('goods_golden_level',request()->level)   ;
            }
            return $query;
        });
        return $this->success([
            'goods' => $roomGoods,
            'roomLevel' => $roomLevels
        ]);
    }


    public function show($id)
    {
        $roomGoods = $this->rgoods->find($id);
        $member = $roomGoods->member;
        return $this->success([
            'room' => $roomGoods,
            'member' => $member,
        ]);
    }
}
